//
//  LeftAddViewController.m
//  Daybook
//
//  Created by dllo on 16/5/19.
//  Copyright © 2016年 zhangbin. All rights reserved.
//


#import "LeftAddViewController.h"
#import "Common.h"

@interface LeftAddViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate,
    AccountBookStyleViewControllerDelegate,
    PanelDetailViewControllerDelegate,
    UITextFieldDelegate
>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) BaseView *topView;
@property (nonatomic, strong) BaseButton *backButton;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) BaseButton *finishButton;
@property (nonatomic, assign) NSInteger firstIndex;
@property (nonatomic, strong) NSArray *picArr;
@property (nonatomic, assign) NSInteger secondIndex;
@property (nonatomic, strong) NSArray *themeArray;

@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *cover;
@property (nonatomic, strong) NSString *background;
@property (nonatomic, strong) AccountBookModel *model;
@property (nonatomic, strong) NSIndexPath *indexPath;
@property (nonatomic, strong) AccountBookNameCell *cell;

@property (nonatomic, strong) UIView *lineView;

@end
@implementation LeftAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createnavigationView];
    [self createView];
    
}



- (void)createnavigationView {
    
    
    
    self.picArr = @[@"AccountBookCover1.png", @"AccountBookCover2.png", @"AccountBookCover3.png", @"AccountBookCover4.png", @"AccountBookCover5.png", @"AccountBookCover6.png"];
    
    self.themeArray = @[@"jiandian.jpg", @"bgImage5.jpg", @"zhuangxiu.jpg", @"jiehun.jpg", @"bookTemplateBg_5@3x.png", @"baobao.jpg"];
    self.topView = [[BaseView alloc] initWithFrame:CGRectMake(0, 20, MAIN_WIDTH, 44)];
    [self.view addSubview:_topView];
    _topView.backgroundColor = [UIColor whiteColor];
    
    self.backButton = ({
        BaseButton *tempButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(10 / 414.0 * MAIN_WIDTH, 7, 25 / 414.0 * MAIN_WIDTH, 25 / 414.0 * MAIN_WIDTH);
        [_topView addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        tempButton;
    });
    
    
    self.finishButton = ({
        BaseButton *tempButton = [BaseButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(374 / 414.0 * MAIN_WIDTH, 7, 30 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        [_topView addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"duihao.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        tempButton;
    });
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(MAIN_WIDTH / 2 - 100, 0, 200, 44)];
    
    [_topView addSubview:_titleLabel];
    _titleLabel.text = @"添加账本";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 43, MAIN_WIDTH, 1)];
    [_topView addSubview:_lineView];
    self.lineView.backgroundColor = LIGHT_GRAY;
}
- (void)createView {
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectMake(0, 64, MAIN_WIDTH, MAIN_HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = 100;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
    [self.tableView registerClass:[AccountBookNameCell class] forCellReuseIdentifier:NSStringFromClass([AccountBookNameCell class])];
    [self.tableView registerClass:[AccountBookStyleCell class] forCellReuseIdentifier:NSStringFromClass([AccountBookStyleCell class])];
    [self.tableView registerClass:[PanelCell class] forCellReuseIdentifier:NSStringFromClass([PanelCell class])];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    else {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 50;
    }
    else {
        return 70;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            AccountBookNameCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AccountBookNameCell class]) forIndexPath:indexPath];
            self.indexPath = indexPath;
            cell.nameTextField.placeholder = @"个人账本";
            return cell;
            
        }else {
            AccountBookStyleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([AccountBookStyleCell class]) forIndexPath:indexPath];
            //cell.selectionStyle = UITableViewCellSelectionStyleNone;
            if ( _picArr[_firstIndex] != nil) {
                [cell.picImageView setImage:[UIImage imageNamed:_picArr[self.firstIndex]]];
                self.cover = _picArr[self.firstIndex];
            }
            else {
                self.cover = _picArr[0];
            }
            
            return cell;
            
        }
        
    }
    else {
        PanelCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PanelCell class]) forIndexPath:indexPath];
        if ( _themeArray[_secondIndex] != nil) {
            [cell.picImageView setImage:[UIImage imageNamed:_themeArray[self.secondIndex]]];
            self.background = _themeArray[self.secondIndex];
        }
        else {
            self.background = _themeArray[0];
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.section == 0) {
        if (indexPath.row == 1) {
            AccountBookStyleViewController *accountBookStyleVC = [[AccountBookStyleViewController alloc]init];
            accountBookStyleVC.delegate = self;
            BaseNavigationController *leftAddNaVC = [[BaseNavigationController alloc] initWithRootViewController:accountBookStyleVC];
            [leftAddNaVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            [self presentViewController:leftAddNaVC animated:YES completion:^{
                
                
            }];
            
        }
    }
    else {
        PanelDetailViewController *panelDetailVC = [[PanelDetailViewController alloc]init];
        panelDetailVC.delegate = self;
        BaseNavigationController *leftAddNaVC = [[BaseNavigationController alloc] initWithRootViewController:panelDetailVC];
        [leftAddNaVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
        [self presentViewController:leftAddNaVC animated:YES completion:^{
            
            
        }];
    }
    
}
- (void)sendIndex:(NSInteger)index {
    self.firstIndex = index;
    [_tableView reloadData];
}
- (void)sendThemeIndex:(NSInteger)index {
    self.secondIndex = index;
    [_tableView reloadData];
}


- (void)backButtonAction:(UIButton *)button {
    [self dismissViewControllerAnimated:YES completion:^{
        
       

    }];
}

- (void)finishButtonAction:(UIButton *)button {
    self.model = [[AccountBookModel alloc] init];
    self.cell = (AccountBookNameCell *)[_tableView cellForRowAtIndexPath:_indexPath];
    self.name = _cell.nameTextField.text;
    if ([self.name isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"账本名不能为空" message:nil delegate:nil cancelButtonTitle:@"确认" otherButtonTitles:nil, nil];
        [alert show];
    }else {
        self.model.accountBookName = self.name;
        self.model.accountBookCover = self.cover;
        self.model.accountBookBackground = self.background;
        BOOL isInsert = [[AccountBookSQL shareDataBaseTool] insertModel:_model];
        if (isInsert == YES) {
            [self.delegate addModel:_model];
            [self dismissViewControllerAnimated:YES completion:^{
                
                
            }];
#pragma mark - 创建realm数据库
            NSInteger sqlArrCount = [[AccountBookSQL shareDataBaseTool] select].count;
            NSLog(@"当前创建时数组的总个数: %ld", sqlArrCount);
            [RealmTool createDayBookInRealmFromIndex:[NSString stringWithFormat:@"%ld", sqlArrCount - 1] name:_name];
        }
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
